<?php
declare (strict_types=1);

namespace app\course\service\video\admin;

use app\common\logic\Attach;
use app\common\model\ProductType;
use app\common\service\BaseService;
use app\course\logic\base\admin\CourseSection;
use app\course\logic\video\admin\VideoSection as SectionLogic;
use app\course\model\base\Course;
use app\course\model\video\CourseSectionVideo;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 点播课程课时接口类-后台
 */
class VideoSection extends BaseService
{

    /**
     * @Inject()
     *
     * @var SectionLogic
     */
    private $secLogic;

    /**
     * 课时树
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-30
     */
    public function sectionTree(int $id)
    {
        $data = $this->secLogic->sectionTree($id);
        // 定义字段
        $keyList = [
            'title' => ListItem::make('custom')->title('名称'),
            'price' => ListItem::make('custom')->title('价格(单位:元)'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;
        // 课程信息
        // 是否已经完结
        $list->is_finished = app(\app\course\logic\base\admin\Course::class)->isFinished($id);

        return $list->send();
    }

    /**
     * 分类子集
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-28
     */
    public function getChildren(int $id, int $pid)
    {
        $data = $this->secLogic->sectionTree($id, $pid);

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * 创建课时
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-30
     */
    public function addSection(int $id = 0, int $pid = 0)
    {
        if (Request::isPost()) {
            $data = Request::post();
            $re   = $this->secLogic->addSection($id, $pid, $data);

            return $this->parseToData($re->toArray(), 1, '添加成功');
        }

        /* 创建课程界面配置 */
        $keyList = [
            'title' => FormItem::make()->title('课时名称')->required(),
        ];
        // 最后一个课时允许添加课件与价格
        if ($this->secLogic->isLastSction($id, $pid)) {

            $keyList = $this->paddingKetList($keyList);
        }
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $keyList;
        // 排序
        $form->saveMethod  = 'POST';
        $url               = '/course/admin/video/' . $id . '/section/' . $pid;
        $form->savePostUrl = $url;

        return $form->send();
    }

    /**
     * 修改课时界面
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-27
     *
     * @param int $sid [description]
     * @param int $id  [description]
     * @return   [type]                              [description]
     */
    public function update(int $id, int $sid)
    {
        if (Request::isPut()) {
            $data    = Request::put();
            $section = SectionLogic::findOrFail($sid);
            $section = $this->secLogic->editSection($section, $data);

            return $this->parseToData($section, 1, '编辑成功');
        }

        $data              = $this->secLogic->where('id', $sid)->findOrFail();
        $data->product_key = ProductType::getProductKeyById(Course::getProductIdByCourseType($data->getAttr('course_type')));
        /* 创建课程界面配置 */
        $keyList = [
            'title' => FormItem::make()->title('课时名称')->required(),
        ];
        if ($this->secLogic->isLastSction($id, $data->pid)) {

            $keyList = $this->paddingKetList($keyList);

            // 查询课时关联数据
            $courseData                  = app(CourseSection::class)->parseCourseHour($data);
            $data->course_data_id_attach = $courseData;
            $data->course_data_type      = $courseData->data_type;
            $data->course_data_id        = $courseData->data_type == 3 ? $courseData->data_txt : $courseData->attach_id;
            // if ($data->getAttr('material_id')) {
            //     $data->material_id_list = [
            //         [
            //             'id'   => $data->getAttr('material_id'),
            //             'name' => app(Attach::class)->where('id', $data->getAttr('material_id'))->value('filename'),
            //         ],
            //     ];
            // }

        }
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $url               = '/course/admin/video/' . $id . '/section/' . $sid;
        $form->savePostUrl = $url;

        $form->datas = $data;

        return $form->send();

    }

    /**
     * 填充表单字段
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-06
     *
     * @param array $keyList [description]
     * @return   [type]                                  [description]
     */
    protected function paddingKetList(array $keyList)
    {
        $keyList['course_data_type'] = FormItem::make('radio')->title('课时类型')->options(app(CourseSectionVideo::class)->getDataTypeOptions())->required();
        $keyList['course_data_id']   = FormItem::make('custom')->title('课件')->required();
        $keyList['price']            = FormItem::make('inputNumber')->title('课时价格')->min(0)->precision(2);
        $keyList['audition']         = FormItem::make('custom')->title('试看/试听')->slot('%')->css(['width' => '126px'])->help('试看/试听百分比<br />设置为0时，表示不支持试看/试听');
        // $keyList['material_id']           = FormItem::make('upload')->title('课时资料')->limit(1)->autoupload()->help('课件，学员可下载;允许文件类型：word、excel、PPT、TXT、PDF、zip、tar');
        $keyList['completion_conditions'] = FormItem::make('custom')->title('完成条件')->help('1. 累计学习时长达标后即学完课时<br />2. 0表示点击即完成');
        $keyList['class_hour']            = FormItem::make('inputNumber')->title('学时')->min(0);

        return $keyList;
    }

    /**
     * 编辑指定的字段
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-27
     */
    public function updateSort($sid)
    {
        $value = Request::put('best_sort/d');
        $video = $this->secLogic->updateSort($sid, $value);

        return $this->parseToData($video, 1, '编辑成功');
    }

    /**
     * 删除课时
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-04-28
     *
     * @param int $sid [description]
     * @return   [type]                              [description]
     */
    public function delete(int $sid)
    {
        $res = $this->secLogic->sectionDelete($sid);

        return $this->parseToData($res, 1, '删除成功');

    }

}
